package com.shop2cn.iapi.sqidcard.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqidcard.parameter.SearchLastestPurchaserIdcardInfoReq;
import com.shop2cn.iapi.sqidcard.parameter.resp.IdcardInfoVo;
import com.shop2cn.iapi.sqidcard.service.SearchLastestPurchaserIdcardInfoCall;
import com.ymatou.common.lang.CipherUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.sqidcard.IdcardInfo;
import com.ymttest.database.sqlwapper.sqidcard.IdcardInfoWrapper;

public class Ts_SearchLastestPurchaserIdcardInfo {
	
	//java接口
	private static SearchLastestPurchaserIdcardInfoReq searchLastestPurchaserIdcardInfoReq;
	private static SearchLastestPurchaserIdcardInfoCall searchLastestPurchaserIdcardInfoCall;
	
	private static IdcardInfoWrapper idcardInfoWrapper = new IdcardInfoWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据userId获取最近成功推送的身份证信息");
	}

	@Before
	public void caseUp() {
		searchLastestPurchaserIdcardInfoReq = new SearchLastestPurchaserIdcardInfoReq();
		searchLastestPurchaserIdcardInfoCall = new SearchLastestPurchaserIdcardInfoCall();	
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(searchLastestPurchaserIdcardInfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb() throws Exception {
		
		String newKey = "123456789";

		IdcardInfo expectResult = idcardInfoWrapper
				.selectLastestIdcardInfo(searchLastestPurchaserIdcardInfoReq
						.getUserId());
		IdcardInfoVo actualResult = searchLastestPurchaserIdcardInfoCall
				.getIdcardInfoVo();

		Logger.verifyEquals(expectResult.getIdcard_name(),
				actualResult.getIdcardName(), "验证Idcard_name");
		Logger.verifyEquals(
				CipherUtil.decryptAES(newKey, expectResult.getIdcard_no()),
				actualResult.getIdcardNo(), "验证Idcard_no");
		Logger.verifyEquals(expectResult.getUser_id(),
				actualResult.getUserId(), "验证User_id");
		Logger.verifyEquals(expectResult.getIdcard_id(),
				actualResult.getIdcardId(), "验证Idcard_id");
		Logger.verifyEquals(expectResult.getPush_status(),
				actualResult.getPushStatus(), "验证Push_status");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SearchLastestPurchaserIdcardInfo_001() {
		Logger.start(true, "根据userId获取最近成功推送的身份证信息");
		
		try {
			
			searchLastestPurchaserIdcardInfoReq.setUserId(88149400000L);
			searchLastestPurchaserIdcardInfoReq.setReceiverPhone("1");
			searchLastestPurchaserIdcardInfoReq.setReceiver("商迟");
			
			searchLastestPurchaserIdcardInfoCall.setData(searchLastestPurchaserIdcardInfoReq);
			searchLastestPurchaserIdcardInfoCall.callService();
			
			Logger.verifyEquals(true, searchLastestPurchaserIdcardInfoCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SearchLastestPurchaserIdcardInfo_002() {
		Logger.start(true, "不输入userID");
		
		try {
			searchLastestPurchaserIdcardInfoReq.setUserId(null);
			searchLastestPurchaserIdcardInfoReq.setReceiverPhone("1");
			searchLastestPurchaserIdcardInfoReq.setReceiver("商迟");
			
			searchLastestPurchaserIdcardInfoCall.setData(searchLastestPurchaserIdcardInfoReq);
			searchLastestPurchaserIdcardInfoCall.callService();
			
			Logger.verifyEquals(false, searchLastestPurchaserIdcardInfoCall.getSuccess(), "验证success");
			Logger.verifyEquals(100, searchLastestPurchaserIdcardInfoCall.getErrorCode(), "验证errorCode");
			Logger.verifyEquals("userId:must be greater than or equal to 1",
					searchLastestPurchaserIdcardInfoCall.getMsg(), "验证errorMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SearchLastestPurchaserIdcardInfo_003() {
		Logger.start(true, "不输入ReceiverPhone");
		
		try {
			searchLastestPurchaserIdcardInfoReq.setUserId(88149400000L);
			searchLastestPurchaserIdcardInfoReq.setReceiverPhone(null);
			searchLastestPurchaserIdcardInfoReq.setReceiver("商迟");
			
			searchLastestPurchaserIdcardInfoCall.setData(searchLastestPurchaserIdcardInfoReq);
			searchLastestPurchaserIdcardInfoCall.callService();
			
			Logger.verifyEquals(true, searchLastestPurchaserIdcardInfoCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SearchLastestPurchaserIdcardInfo_004() {
		Logger.start(true, "不输入Receiver");
		
		try {
			searchLastestPurchaserIdcardInfoReq.setUserId(88149400000L);
			searchLastestPurchaserIdcardInfoReq.setReceiverPhone("1");
			searchLastestPurchaserIdcardInfoReq.setReceiver("");
			
			searchLastestPurchaserIdcardInfoCall.setData(searchLastestPurchaserIdcardInfoReq);
			searchLastestPurchaserIdcardInfoCall.callService();
			
			Logger.verifyEquals(true, searchLastestPurchaserIdcardInfoCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SearchLastestPurchaserIdcardInfo_006() {
		Logger.start(true, "输入的userId、receiverPhone、receiver均不存在");
		
		try {
			searchLastestPurchaserIdcardInfoReq.setUserId(52130185019284L);
			searchLastestPurchaserIdcardInfoReq.setReceiverPhone("13933221192");
			searchLastestPurchaserIdcardInfoReq.setReceiver("感到尴尬");
			
			searchLastestPurchaserIdcardInfoCall.setData(searchLastestPurchaserIdcardInfoReq);
			searchLastestPurchaserIdcardInfoCall.callService();
			
			Logger.verifyEquals(true, searchLastestPurchaserIdcardInfoCall.getSuccess(), "验证success");
			
			IdcardInfoWrapper idcardInfoWrapper = new IdcardInfoWrapper();
			
			IdcardInfo expectResult = idcardInfoWrapper
					.selectLastestIdcardInfo(searchLastestPurchaserIdcardInfoReq
							.getUserId());
			
			Logger.verifyEquals(null, expectResult, "验证数据库数据");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
